<?php

/* Email.php
 *
 * (C) 2012, StaySimple CMS team.
 *
 * This file is part of StaySimple.
 * http://code.google.com/p/stay-simple-cms/
 *
 * StaySimple is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * StaySimple is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with StaySimple.  If not, see <http://www.gnu.org/licenses/>.
 */

class Email {

    private $to;
    private $address;
    private $from;
    private $message;
    private $subject;

    /**
     * Creates an email object that will use the system settings to send e-mails.
     * 
     * @param string $to The name of the user to receive the e-mail.
     * @param string $address The destination address.
     * @param string $from The name for the user or system sending the e-mail, 
     * the address will be the one defined in the settings file.
     * @param string $message The message to send.
     */
    public function __construct($to, $address, $from, $subject, $message) {
        $this->to = $to;
        $this->address = $address;
        $this->from = $from;
        $this->message = $message;
        $this->subject = $subject;
    }

    /**
     * Sends an this e-mail using PHPMailer and the system's configuration.
     * 
     * @throws InvalidEmailConfigurationException
     * @throws EmailException 
     */
    public function send() {
        $from = Config::getInstance()->get('email.email');
        if (!$from) {
            throw new InvalidEmailConfigurationException('Required parameters are not set.');
        }

        $port = Config::getInstance()->get('email.smtp.port');
        if (!$port) {
            $port = 25;
        }

        $lang = explode('_', Config::getInstance()->get('locale.language'));
        $lang = $lang[0];

        $mailer = new PHPMailer();
        $mailer->SetLanguage($lang, APPROOT . '/core/helpser/phpmailer/languages/');

        $mailer->AddAddress($this->address, $this->to);
        $mailer->From = Config::getInstance()->get('email.email');

        $mailer->Body = $this->message;

        if (Config::getInstance()->get('email.html')) {
            $mailer->IsHTML(true);
            $mailer->AltBody = strip_tags($this->message);
        }

        if (Config::getInstance()->get('email.usesmtp')) {
            $username = Config::getInstance()->get('email.smtp.username');
            $password = Config::getInstance()->get('email.smtp.password');
            $host = Config::getInstance()->get('email.smtp.host');

            if (!$username || !$password || !$host) {
                throw new InvalidEmailConfigurationException('Required parameters are not set.');
            }

            $mailer->IsSMTP(true);
            $mailer->Username = $username;
            $mailer->Password = $password;
            $mailer->Port = $port;
            $mailer->Host = $host;
            $mailer->SMTPSecure = Config::getInstance()->get('email.smtp.prefix');
        }

        try {
            $mailer->Send();
        } catch (phpmailerException $pe) {
            throw new EmailException('E-mail not sent due to error.', $pe);
        }
    }

}